package cn.edu.gcu.lost.sys.controller;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.gcu.lost.sys.entity.Menu;
import cn.edu.gcu.lost.sys.entity.User;
import cn.edu.gcu.lost.sys.service.MenuService;
import cn.edu.gcu.lost.sys.util.UserUtils;

@Controller
@RequestMapping("menu")
public class MenuController {
	
	@Autowired
	private MenuService menuService;
	
	@ResponseBody
	@RequestMapping("findList")
	@RequiresPermissions("sys:menu")
	public Object findList() {
		Map<String, Object> resultMap = new HashMap<>();
		Map<String, Object> dataMap = new HashMap<>();
		dataMap.put("menuList",menuService.findList());
		resultMap.put("result", "success");
		resultMap.put("data",dataMap);
		return resultMap;
	}
	
	@ResponseBody
	@RequestMapping("save")
	@RequiresPermissions("sys:menu")
	public Object save(Menu menu) {
		Map<String, Object> resultMap = new HashMap<>();
		if(menuService.save(menu)) {
			resultMap.put("result", "success");
			resultMap.put("data", null);
		}else {
			resultMap.put("result", "faild");
			resultMap.put("cause", "save process error");
		}
		return resultMap;
	}
	
	@ResponseBody
	@RequestMapping("delete")
	@RequiresPermissions("sys:menu")
	public Object delete(Menu menu) {
		Map<String, Object> resultMap = new HashMap<>();
		if(menuService.delete(menu)) {
			resultMap.put("result", "success");
			resultMap.put("data", null);
		}else {
			resultMap.put("result", "faild");
			resultMap.put("cause", "delete process error");
		}
		return resultMap;
	}
	
	@ResponseBody
	@RequestMapping("findSelfMenu")
	public Object findSelfMenu() {
		Map<String, Object> resultMap = new HashMap<>();
		Map<String, Object> dataMap = new HashMap<>();
		dataMap.put("menuList", UserUtils.getMenuList());
		resultMap.put("result", "success");
		resultMap.put("data", dataMap);
		return resultMap;
	}
	
	@ResponseBody
	@RequestMapping("findDetail")
	@RequiresPermissions("sys:menu")
	public Object findDetail(Menu menu) {
		Map<String, Object> resultMap = new HashMap<>();
		if(StringUtils.isBlank(menu.getId())) {
			resultMap.put("result", "faild");
			resultMap.put("cause", "param error");
		}else {
			resultMap.put("result", "success");
			resultMap.put("data",menuService.findDetail(menu));
		}
		return resultMap;
	}
}
